<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
      <el-form-item label="课程编码" prop="courseCode">
        <el-input
          v-model="queryParams.courseCode"
          placeholder="请输入课程编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="课程名称" prop="courseName">
        <el-input
          v-model="queryParams.courseName"
          placeholder="请输入课程名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="课程备注" prop="courseRemake">
        <el-input
          v-model="queryParams.courseRemake"
          placeholder="请输入课程备注"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="courseList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="课程ID
"
        align="center"
        prop="courseId"
      />
      <el-table-column label="老师名称" align="center" prop="teacherId" />
      <el-table-column label="课程编码" align="center" prop="courseCode" />
      <el-table-column label="课程名称" align="center" prop="courseName" />
      <el-table-column label="课程备注" align="center" prop="courseRemake" />
      <!-- <el-table-column label="备注" align="center" prop="remark" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.remark"
            v-hasPermi="['zua:course:edit']"
            type="primary"
            icon="el-icon-edit"
            @click="chooseCourse(scope.row)"
          >选择课程</el-button>
          <el-button
            v-else
            type="success"
            disabled
          >已选课</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listCourseByStudentId } from '@/api/zua/course'
import { getUserProfile } from '@/api/system/user'
import { addGrade, listGradeStudent } from '@/api/zua/grade'

export default {
  name: 'Course',
  components: {
  },
  data() {
    return {
      date: {
        studentId: null,
        studentNickName: null,
        courseId: null
      },
      listGrade: [],
      buttonShow: true,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // course表格数据
      courseList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        teacherId: null,
        courseCode: null,
        courseName: null,
        courseRemake: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      // 老师参数
      sexOptions: [
      ]
    }
  },
  async created() {
    // 获取当前登录用户
    await this.getUser()
    // 查询所有的选课信息
    await this.getGradeLiat()
    // 查询课程列表
    await this.getList()
    // 遍历成绩表
    this.listGrade.map(item => {
      // 遍历课程表
      const a = item.courseId
      const b = item.studentId
      this.courseList.map(item2 => {
        if (this.date.studentNickName === b && a === item2.courseName) {
          item2.remark = false
        }
      })
    })
  },
  methods: {
    // 获取所有的选课信息
    async getGradeLiat() {
      const res = await listGradeStudent()
      this.listGrade = res.rows
    },

    // 获取当前登录信息
    async getUser() {
      const res = await getUserProfile()
      this.date.studentId = res.data.userId
      this.date.studentNickName = res.data.nickName
    },
    /** 查询course列表 */
    async getList() {
      this.loading = true
      const res = await listCourseByStudentId(this.queryParams, this.date.studentId)
      this.courseList = res.rows
      this.courseList.map(item => {
        item.remark = true
      })
      this.total = res.total
      this.loading = false
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        courseId: null,
        teacherId: null,
        courseCode: null,
        courseName: null,
        courseRemake: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.courseId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    chooseCourse(row) {
      this.date.courseId = row.courseId
      this.$confirm('你确定要选择这门课程吗？选择以后不能退选', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // this.buttonShow = false
        row.remark = false
        addGrade(this.date).then((res) => {
          this.$message({
            type: 'success',
            message: '选课成功!'
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        })
      })
    }
  }
}
</script>
